using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Windows;
using System.Windows.Forms;
using Common;
using CustomChart;
using Infragistics.UltraChart.Data.Series;
using Infragistics.UltraChart.Resources.Appearance;
using Infragistics.UltraChart.Shared.Styles;
using SExperiment;
using SExperiment.ExperimentResult;
using SProject.Paramter;
using SWellPlate;

namespace MainView.MainControls.Chart
{
	public class PeekCurveChart : BaseMeltCurveChart
	{
		private const string KeyPeekHightThreshold = "PeekHightThreshold";

		private const string KeyMinTemperature = "MinTemperature";

		private const string KeyMaxTemperature = "MaxTemperature";

		public static readonly DependencyProperty ShowPeakProperty = DependencyProperty.Register("ShowPeak", typeof(bool), typeof(PeekCurveChart), new UIPropertyMetadata(true, OnShowPeakChanged));

		public static readonly DependencyProperty InvertDataProperty = DependencyProperty.Register("InvertData", typeof(bool), typeof(PeekCurveChart), new UIPropertyMetadata(false, OnInvertDataChanged));

		private ToolStripMenuItem itemPeakVisible = new ToolStripMenuItem();

		private ToolStripMenuItem itemMeltPeekInvertData = new ToolStripMenuItem();

		public bool ShowPeak
		{
			get
			{
				return (bool)GetValue(ShowPeakProperty);
			}
			set
			{
				SetValue(ShowPeakProperty, value);
			}
		}

		public bool InvertData
		{
			get
			{
				return (bool)GetValue(InvertDataProperty);
			}
			set
			{
				SetValue(InvertDataProperty, value);
			}
		}

		private static void OnShowPeakChanged(object sender, DependencyPropertyChangedEventArgs args)
		{
			PeekCurveChart peekCurveChart = sender as PeekCurveChart;
			if (peekCurveChart != null)
			{
				peekCurveChart.RefreshPeakPoint();
			}
		}

		private static void OnInvertDataChanged(object sender, DependencyPropertyChangedEventArgs args)
		{
			PeekCurveChart peekCurveChart = sender as PeekCurveChart;
			if (peekCurveChart != null)
			{
				peekCurveChart.InvertAxixY();
				peekCurveChart.RefreshSeries(true);
			}
		}

		public PeekCurveChart()
		{
			base.ThresholdVisible = true;
			base.Margin = new Thickness(1.0);
			base.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
			base.VerticalAlignment = VerticalAlignment.Stretch;
			SetResourceReference(BaseCurveChart.CaptionProperty, "peek_curve");
			SetResourceReference(BaseCurveChart.TitleXProperty, "Temperature");
			SetResourceReference(BaseCurveChart.TitleYProperty, "peek_curve_title_y");
			base.MinY = 0.0;
			base.MaxY = 1000.0;
			base.TitleXFormat = "0";
			base.TitleYFormat = "0.000";
			base.UltraChart.TooltipsFromatX = "0.00";
			base.UltraChart.SetScatterIconSize(SymbolIcon.Character, SymbolIconSize.Small, 'I');
			base.UltraChart.SetScatter2IconSize(SymbolIcon.Character, SymbolIconSize.Small, 'x');
		}

		public override string GetToolTipFormatX()
		{
			return "0.00";
		}

		public override Curve GetCurve(SampleTargetItem item)
		{
			if (item.Result is MeltingTargetResult)
			{
				if (!InvertData)
				{
					return ((MeltingTargetResult)item.Result).PeakCurve;
				}
				Curve curve = new Curve();
				{
					foreach (Dot item2 in ((MeltingTargetResult)item.Result).PeakCurve.CurvePoint)
					{
						curve.CurvePoint.Add(new Dot
						{
							X = item2.X,
							Y = 0.0 - item2.Y
						});
					}
					return curve;
				}
			}
			return null;
		}

		public override void RefreshSeries(bool refreshData = false)
		{
			base.RefreshSeries(refreshData);
			RefreshPeakPoint();
		}

		private void RefreshPeakPoint()
		{
			base.UltraChart.ClearAllScatterSeries();
			base.UltraChart.ClearAllScatter2Series();
			if (!ShowPeak)
			{
				return;
			}
			XYSeriesEx xYSeriesEx = new XYSeriesEx();
			XYSeriesEx xYSeriesEx2 = new XYSeriesEx();
			xYSeriesEx.PEs.Add(new PaintElement());
			xYSeriesEx.PEs[0].Fill = Color.Red;
			xYSeriesEx.Visible = true;
			xYSeriesEx2.PEs.Add(new PaintElement());
			xYSeriesEx2.PEs[0].Fill = Color.Red;
			xYSeriesEx2.Visible = true;
			foreach (ChartData displayChartDatum in _DisplayChartData)
			{
				foreach (MeltPeak peakValue in ((MeltingTargetResult)displayChartDatum.SampleTargetItem.Result).PeakValues)
				{
					if (peakValue.IsUnknown)
					{
						xYSeriesEx2.Points.Add(new XYDataPoint(peakValue.Tm, InvertData ? (0.0 - peakValue.YValue) : peakValue.YValue, "", false));
					}
					else
					{
						xYSeriesEx.Points.Add(new XYDataPoint(peakValue.Tm, InvertData ? (0.0 - peakValue.YValue) : peakValue.YValue, "", false));
					}
				}
			}
			base.UltraChart.AddScatterSeries(xYSeriesEx);
			base.UltraChart.AddScatter2Series(xYSeriesEx2);
		}

		protected override void InitMenu()
		{
			base.UltraChart.ContentMenu.Opening += ContentMenu_Opening;
			base.UltraChart.ContentMenu.Items.Add(new ToolStripSeparator());
			itemPeakVisible.Text = Utility.GetMessage("peak_point_visible");
			itemPeakVisible.Click += PeakVisible_Click;
			base.UltraChart.ContentMenu.Items.Add(itemPeakVisible);
			itemMeltPeekInvertData.Text = Utility.GetMessage("InvertData");
			itemMeltPeekInvertData.Click += InvertData_Click;
			base.UltraChart.ContentMenu.Items.Add(itemMeltPeekInvertData);
		}

		private void ContentMenu_Opening(object sender, CancelEventArgs e)
		{
			for (int i = 0; i < base.UltraChart.ContentMenu.Items.Count; i++)
			{
				ToolStripMenuItem toolStripMenuItem = base.UltraChart.ContentMenu.Items[i] as ToolStripMenuItem;
				if (toolStripMenuItem != null)
				{
					if (toolStripMenuItem.Text == Utility.GetMessage("peak_point_visible"))
					{
						toolStripMenuItem.Checked = ShowPeak;
					}
					else if (toolStripMenuItem.Text == Utility.GetMessage("InvertData"))
					{
						toolStripMenuItem.Checked = InvertData;
					}
				}
			}
		}

		private void InvertAxixY()
		{
			double maxY = base.UltraChart.MaxY;
			SetMaxY(0.0 - base.UltraChart.MinY);
			SetMinY(0.0 - maxY);
			SetResourceReference(BaseCurveChart.TitleYProperty, InvertData ? "peek_curve_invert_title_y" : "peek_curve_title_y");
		}

		protected void PeakVisible_Click(object sender, EventArgs e)
		{
			ShowPeak = !ShowPeak;
		}

		protected void InvertData_Click(object sender, EventArgs e)
		{
			InvertData = !InvertData;
		}

		protected override void CreateThresholdSeries(BasicOptionItem item, ChartData chartData)
		{
			RegisterSubsetParamterEvent(chartData);
			List<XYSeriesEx> list = _ThresholdSeriesList.Where((XYSeriesEx s) => s.Tag == item).Cast<XYSeriesEx>().ToList();
			if (item != null && list.Count() == 0)
			{
				item.Event += base.BasicOptionItem_Event;
				if (item is MeltingOptionItem)
				{
					double minTemperature = ((MeltingOptionItem)item).MinTemperature;
					XYSeriesEx xYSeriesEx = new XYSeriesEx();
					xYSeriesEx.Visible = false;
					xYSeriesEx.PEs.Add(new PaintElement());
					xYSeriesEx.PEs[0].Fill = GetThresholdSeriesColor(item.ChannelNo);
					xYSeriesEx.Tag = item;
					xYSeriesEx.Key = "MinTemperature";
					xYSeriesEx.Points.Add(new XYDataPoint(minTemperature, base.MinY, "", false));
					xYSeriesEx.Points.Add(new XYDataPoint(minTemperature, base.MaxY, "", false));
					_ThresholdSeriesList.Add(xYSeriesEx);
					base.UltraChart.AddThresholdSeries(xYSeriesEx);
					list.Add(xYSeriesEx);
					minTemperature = ((MeltingOptionItem)item).MaxTemperature;
					xYSeriesEx = new XYSeriesEx();
					xYSeriesEx.Visible = false;
					xYSeriesEx.PEs.Add(new PaintElement());
					xYSeriesEx.PEs[0].Fill = GetThresholdSeriesColor(item.ChannelNo);
					xYSeriesEx.Tag = item;
					xYSeriesEx.Key = "MaxTemperature";
					xYSeriesEx.Points.Add(new XYDataPoint(minTemperature, base.MinY, "", false));
					xYSeriesEx.Points.Add(new XYDataPoint(minTemperature, base.MaxY, "", false));
					_ThresholdSeriesList.Add(xYSeriesEx);
					base.UltraChart.AddThresholdSeries(xYSeriesEx);
				}
			}
		}

		protected override void ResetThresholdSeriesPoint()
		{
			foreach (XYSeriesEx thresholdSeries in _ThresholdSeriesList)
			{
				XYDataPointCollection points = thresholdSeries.Points;
				if (points.Count > 1)
				{
					if (thresholdSeries.Key == "PeekHightThreshold")
					{
						points[0].ValueX = base.UltraChart.GetCurrentMinX();
						points[1].ValueX = base.UltraChart.GetCurrentMaxX();
					}
					else if (thresholdSeries.Key == "MinTemperature" || thresholdSeries.Key == "MaxTemperature")
					{
						points[0].ValueY = base.UltraChart.GetCurrentMinY();
						points[1].ValueY = base.UltraChart.GetCurrentMaxY();
					}
				}
			}
		}

		protected override void RefreshThresholdValue(BasicOptionItem item, string property)
		{
			if (!(property == "PeekHightThreshold") && !(property == "MinTemperature") && !(property == "MaxTemperature"))
			{
				return;
			}
			XYSeriesEx xYSeriesEx = _ThresholdSeriesList.Where((XYSeriesEx s) => s.Tag == item && s.Key == property).FirstOrDefault();
			if (xYSeriesEx == null)
			{
				return;
			}
			XYDataPointCollection points = xYSeriesEx.Points;
			for (int i = 0; i < points.Count; i++)
			{
				if (property == "PeekHightThreshold")
				{
					points[i].ValueY = ((MeltingOptionItem)item).PeekHightThreshold;
				}
				else if (property == "MinTemperature")
				{
					points[i].ValueX = ((MeltingOptionItem)item).MinTemperature;
				}
				else if (property == "MaxTemperature")
				{
					points[i].ValueX = ((MeltingOptionItem)item).MaxTemperature;
				}
			}
			if (xYSeriesEx.Visible)
			{
				xYSeriesEx.Visible = false;
				xYSeriesEx.Visible = true;
			}
			RaiseItemUpdateEvent();
		}

		protected override string GetThresholdToolTips(XYSeriesEx series)
		{
			string result = base.GetThresholdToolTips(series);
			if (series.Tag is BasicOptionItem)
			{
				double d = double.NaN;
				BasicOptionItem basicOptionItem = (BasicOptionItem)series.Tag;
				if (series.Key == "PeekHightThreshold")
				{
					d = ((MeltingOptionItem)basicOptionItem).PeekHightThreshold;
				}
				else if (series.Key == "MinTemperature")
				{
					d = ((MeltingOptionItem)basicOptionItem).MinTemperature;
				}
				else if (series.Key == "MaxTemperature")
				{
					d = ((MeltingOptionItem)basicOptionItem).MaxTemperature;
				}
				if (!double.IsNaN(d))
				{
					result = basicOptionItem.TargetName + Utility.GetMessage("Colon") + d.ToString("n1");
				}
			}
			return result;
		}

		protected override void SetThreshold(XYSeriesEx series, int x, int y)
		{
			if (!(series.Tag is BasicOptionItem))
			{
				return;
			}
			BasicOptionItem basicOptionItem = (BasicOptionItem)series.Tag;
			if (series.Key == "PeekHightThreshold")
			{
				((MeltingOptionItem)basicOptionItem).PeekHightThreshold = base.UltraChart.GetLocationYData(y);
			}
			else if (series.Key == "MinTemperature")
			{
				double locationXData = base.UltraChart.GetLocationXData(x);
				if (locationXData < ((MeltingOptionItem)basicOptionItem).MaxTemperature)
				{
					((MeltingOptionItem)basicOptionItem).MinTemperature = locationXData;
				}
			}
			else if (series.Key == "MaxTemperature")
			{
				double locationXData2 = base.UltraChart.GetLocationXData(x);
				if (locationXData2 > ((MeltingOptionItem)basicOptionItem).MinTemperature)
				{
					((MeltingOptionItem)basicOptionItem).MaxTemperature = locationXData2;
				}
			}
		}

		protected override Cursor GetThresholdDragCursor(string key)
		{
			switch (key)
			{
			case "PeekHightThreshold":
				return Cursors.HSplit;
			case "MinTemperature":
			case "MaxTemperature":
				return Cursors.VSplit;
			default:
				return GetThresholdDragCursor(key);
			}
		}

		protected override bool CanThresholdChanged(BasicOptionItem item)
		{
			return _ThresholdSeriesList.Where((XYSeriesEx s) => s.Visible).Count() == 2;
		}
	}
}
